package rs.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class TimeUtil {
	
	/**
	 * 一天的毫秒数
	 */
	public static final long PERIOD_DAY = 24 * 60 * 60 * 1000;

	public static final long PERIOD_MINUTE = 60 * 1000;
	
	private static Calendar calendar = Calendar.getInstance();
	
	private static HashMap<String, Calendar> incCalendars = new HashMap<String, Calendar>();
	
	
	//private static Calendar incSchedule = Calendar.getInstance();
	
	/*public static long getScheduleTimeFromTask(Task task){
		long ret = 0;
		String timeStr = task.getScheduleTime();
		if(timeStr != null){
			String[] times = timeStr.split(":");
			if(times.length == 3){
				int hour = Integer.parseInt(times[0]);
				int minute = Integer.parseInt(times[1]);
				int second = Integer.parseInt(times[2]);
				return getFutureTime(hour, minute, second);
			}
		} 
		ret = new Date().getTime() + task.getDelayTime();
		return ret;
	}*/
	
	public static long getFutureTime(int hour, int minute, int second){
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, 0);
		if(calendar.getTimeInMillis() < new Date().getTime()){
			calendar.add(Calendar.DATE, 1);
		}
		return calendar.getTimeInMillis();
	}
	
	/**
	 * @param increaseMiliSecond
	 * @param key
	 * @return
	 */
	public static String getIncreaseTimeStr(int increaseMiliSecond, String key){
		if(key == null){key = "Global";}
		Calendar incSchedule = incCalendars.get(key);
		if(incSchedule == null){
			incSchedule = Calendar.getInstance();
		}
		incSchedule.add(Calendar.MILLISECOND, increaseMiliSecond);
		incCalendars.put(key, incSchedule);
		return longToString(incSchedule);
	}
	
	
	public static String longToString(Calendar c){
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		return hour + ":" + minute + ":" + second;
	}
	
	public static long now(){
		return System.currentTimeMillis();
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}
}
